package com.ruoyi.bl.controller;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bl.domain.Historychat;
import com.ruoyi.bl.service.IHistorychatService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 历史对话Controller
 * 
 * @author zhanghao
 * @date 2025-03-04
 */
@RestController
@RequestMapping("/bl/historychat")
public class HistorychatController extends BaseController
{
    @Autowired
    private IHistorychatService historychatService;

    /**
     * 查询历史对话列表
     */
//    @PreAuthorize("@ss.hasPermi('bl:historychat:list')")
    @GetMapping("/list")
    public AjaxResult list(Historychat historychat)
    {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        historychat.setUserId(loginUser.getUserId());
        List<Historychat> list = historychatService.selectHistorychatList(historychat);
        Map<String, List<Historychat>> categorizedChats =  list.stream()
                .collect(Collectors.groupingBy(Historychat::getChatId));
        return AjaxResult.success(categorizedChats);
    }

    /**
     * 导出历史对话列表
     */
    /*@PreAuthorize("@ss.hasPermi('bl:historychat:export')")*/
    @Log(title = "历史对话", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Historychat historychat)
    {
        List<Historychat> list = historychatService.selectHistorychatList(historychat);
        ExcelUtil<Historychat> util = new ExcelUtil<Historychat>(Historychat.class);
        util.exportExcel(response, list, "历史对话数据");
    }

    /**
     * 获取历史对话详细信息
     */
//    @PreAuthorize("@ss.hasPermi('bl:historychat:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(historychatService.selectHistorychatById(id));
    }

    /**
     * 新增历史对话
     */
    /*@PreAuthorize("@ss.hasPermi('bl:historychat:add')")*/
    @Log(title = "历史对话", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Historychat historychat)
    {
        historychat.setTime(LocalDateTime.now());
        return toAjax(historychatService.insertHistorychat(historychat));
    }

    /**
     * 修改历史对话
     */
    @PreAuthorize("@ss.hasPermi('bl:historychat:edit')")
    @Log(title = "历史对话", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Historychat historychat)
    {
        return toAjax(historychatService.updateHistorychat(historychat));
    }

    /**
     * 删除历史对话
     */
    @PreAuthorize("@ss.hasPermi('bl:historychat:remove')")
    @Log(title = "历史对话", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(historychatService.deleteHistorychatByIds(ids));
    }
}
